﻿<?php require_once("includes/connection.php"); ?>
<?php require_once("includes/functions.php"); ?>
<?php include("includes/header.php"); ?>
<?php //check if GET set
	if (!isset($_GET['userid'])) {
		$to = "client.php";
		redirect_to($to);
		exit;  
		};
 ?>
 <?php //check if paskatinimaiid set
	if (!isset($_GET['paskatinimaiid'])) {
		$paskatinimaiid = 0;  
		}else{
		$paskatinimaiid = ($_GET['paskatinimaiid']);
		//gaunam paskatinimo detales
		$query = "SELECT * 
			FROM `paskatinimai`
			WHERE paskatinimaiid=";
		$query .=($_GET['paskatinimaiid']);
		$result_set = mysql_query($query, $connection);
		$sabotage = mysql_num_rows($result_set);
		$getpaskatinimai= mysql_fetch_array($result_set);
		//tikrinam ar yra toks paskatinimas ir ar jis priklauso tam klientui
		if(($sabotage==0) or (($getpaskatinimai['userid'])!=($_GET['userid']))){echo "<h1>Sabotage<h1>";};
		
		};
 ?>

<?php //check if user has primary photo

	$query = "SELECT *";
	$query .= " FROM `foto`";
	$query .= " WHERE `foto`.userid =";
	$query .= "'".$_GET['userid']."'";
	$query .= " AND `pagrindine` = 1";
	//echo $query."</br>";
	$result_set = mysql_query($query, $connection);
	$subject = mysql_fetch_array($result_set);
	
	if (($subject['pagrindine']) !=0) {
	$message = "Pagrindine foto jau yra";
	$pagridine_yra = 1;
	$foto_location = $subject['path'];
	$foto_id = $subject['fotoid'];
	
	} else {
	$message = "Pagrindines foto nera";
	$pagridine_yra = 0;
	};
	
	
?>

<?php //Gaunam Pagrindinius kliento duomenys
	
	$query = "SELECT *";
	$query .= " FROM `user`, `lovos`";
	$query .= " WHERE `user`.userid =";
	$query .= "'".$_GET['userid']."'";
	$query .= "AND `user`.lovosid = `lovos`.lovosid";
	
	//echo $query."</br>";
	$result_set = mysql_query($query, $connection);

	$subject = mysql_fetch_array($result_set);

?>

 <?php //procesinam ka gaunam is post
 
if (isset($_POST['submit'])) {
	
	if($paskatinimaiid==0){

	//naujas nuo cia
		if (($_POST['priezastis']) ==0){
			if(trim($_POST['kita']) !=""){
				echo "Hiuston, we have a landing";
				$sql = "INSERT INTO `test`.`paskatinimopriezastys` (`paskatinimopriezastys`) VALUES (";
				$sql .= "'";
				$sql .= trim($_POST['kita']);
				$sql .= "'";
				$sql .= ")";
				echo ">".$sql."<";
				$result = mysql_query($sql, $connection);
				if ($result) {
					$priezastis=mysql_insert_id();
					//echo "QWQ ". $priezastis;
				} else {
					// Display error message.
					$message .= "<p>Client update failed.</p>";
					$message .= "<p>" . mysql_error() . "</p>";
				};
				//$priezastis=1;
			}else{
				$to = "problemos.php";
				redirect_to ($to);};
		}else {
			$priezastis=$_POST['priezastis'];
		};

		
		$sql = "INSERT INTO `paskatinimai` (`userid`, `paskatinimopriezastysid`, `paskrusysid`, `burys`, `svarstdata`, `nuobaudosid`";
		$sql .= ") VALUES (";
		$sql .= $subject['userid'] . ", ";
		$sql .= $priezastis . ", ";
		$sql .= $_POST['paskatinimas'] . ", ";
		$sql .= $subject['burys'] . ", ";
		$sql .= "'".$_POST['svarstymodata']."', ";
		$sql .= $_POST['nuobauda'];
		$sql .= ");";
		
		$result = mysql_query($sql, $connection);
			if ($result) {
			
	//===========================================================//
	//   HZ kur redirect
	//===========================================================//
			
			
				$to = "summary.php?userid=" . $_GET['userid'];
				redirect_to($to);
				// Success!
				//$message .= "</br>Client updated Successsullyy!! </br>";
			} else {
				// Display error message.
				$message .= "<p>Client update failed.</p>";
				$message .= "<p>" . mysql_error() . "</p>";
			};
		//naujas iki cia	
		}else{


		//seno redagavimas nuo cia
		
		
			if (($_POST['priezastis']) ==0){
			if(trim($_POST['kita']) !=""){
				echo "Hiuston, we have a landing";
				$sql = "INSERT INTO `test`.`paskatinimopriezastys` (`paskatinimopriezastys`) VALUES (";
				$sql .= "'";
				$sql .= trim($_POST['kita']);
				$sql .= "'";
				$sql .= ")";
				echo ">".$sql."<";
				$result = mysql_query($sql, $connection);
				if ($result) {
					$priezastis=mysql_insert_id();
					//echo "QWQ ". $priezastis;
				} else {
					// Display error message.
					$message .= "<p>Client update failed.</p>";
					$message .= "<p>" . mysql_error() . "</p>";
				};
				//$priezastis=1;
			}else{
				$to = "problemos.php";
				redirect_to ($to);};
		}else {
			$priezastis=$_POST['priezastis'];
		};

		$sql = "UPDATE `paskatinimai`";
		//$sql = "INSERT INTO `paskatinimai` (`userid`, `paskatinimopriezastysid`, `paskrusysid`, `burys`, `svarstdata`";
		$sql .= " SET";
		$sql .= " `userid`=";
		$sql .= $subject['userid'] . ", ";
		$sql .= " `paskatinimopriezastysid`=";
		$sql .= $priezastis . ", ";
		$sql .= " `paskrusysid`=";
		$sql .= $_POST['paskatinimas'] . ", ";
		$sql .= " `burys`=";
		$sql .= $subject['burys'] . ", ";
		$sql .= " `svarstdata`=";
		$sql .= "'".$_POST['svarstymodata']."'";
		$sql .= " `nuobaudosid`=";
		$sql .= $_POST['nuobauda'];
		//$sql .= ");";
		$sql .= " WHERE `paskatinimaiid`=";
		$sql .= $_GET['paskatinimaiid'];
			
			
		$result = mysql_query($sql, $connection);
			if ($result) {
			
	//===========================================================//
	//   HZ kur redirect
	//===========================================================//
			
			
				$to = "form_preprot.php?svarstdata=" . $_POST['svarstymodata'];
				redirect_to($to);
				// Success!
				// $message .= "</br>Client uuupdated Successsullyy!! </br>";
			} else {
				// Display error message.
				$message .= "<p>Client update failed.</p>";
				$message .= "<p>" . mysql_error() . "</p>";
			};
		
		//seno redagavimas iki cia
		};
};
	
  

 ?>


					<article>
						<h2><?php 
							if($nuobaudosid !=0){
								echo "Paskatinimo redagavimas";
							}else{
								echo "Paskatinimo formavimas";
							};
						
						?></h2>
	<?php echo $message."</br>";?>
	<?php //rodomi pagrindiniai kliento duomenys

	//print_r ($subject) ;
	
	//echo "Vardas: ";
	echo $subject['vardas'];
	echo ' ';
	
	// echo "Pavarde: ";
	echo $subject['pavarde'];
	echo ' ';	
	
	//echo "Bylos Nr.: ";
	echo $subject['bylosnr'];
	echo '</br>';
	$tau_dabar = $subject['tautybeid'];
	$pil_dabar = $subject['pilietybeid'];
	
?>
    <?php //rodom pagrindine foto jeigu yra
	if ($pagridine_yra == 1){ 
		echo "</br> <img src=\"";
		echo $foto_location;
		echo "\" width=\"150\">";
	};
?> 
   
<form action="form_skatinti.php?userid=<?php echo ($_GET['userid']);if($paskatinimaiid !=0){echo "&paskatinimaiid=".$paskatinimaiid;};?>" method="post" name="submit">
  
  </br>
  Svarstymo data: <input name="svarstymodata" type="date" value="<?php 
							if($paskatinimaiid !=0){
								echo ($getpaskatinimai['svarstdata']);
							}else {echo date("Y-m-j");};
						
						?>" placeholder="1900-12-31" required/>
 <br>
  
<br>
  Priezastis: <select name="priezastis" size="1">
			<?php 		$query = "SELECT * 
						FROM `paskatinimopriezastys` 
						ORDER BY paskatinimopriezastysid ASC";
						$pask_set = mysql_query($query, $connection);
						confirm_query($pask_set);
						while ($pask = mysql_fetch_array($pask_set)) {
							echo "<option value=\"". $pask["paskatinimopriezastysid"] . "\"";
							if($paskatinimaiid !=0){
								if(($getpaskatinimai['paskatinimopriezastysid'])==($pask["paskatinimopriezastysid"])){
								echo " selected";
								};
							};
							echo ">" . $pask["paskatinimopriezastys"] . "</option>";
						};

			?> <option value="0">Kita</option>
			</select>
</br>
Kita: <input name="kita" type="text" value="" /> 
					 <small>Pildoma neradus saraše reikiamos priežasties</small>		
<br><br>
  Paskirtas paskatinimas: <select name="paskatinimas" size="1">
			<?php 		$query = "SELECT * 
						FROM `paskatinimorusys` 
						ORDER BY paskrusysid ASC";
						$pask_set = mysql_query($query, $connection);
						confirm_query($pask_set);
						while ($pask = mysql_fetch_array($pask_set)) {
							echo "<option value=\"". $pask["paskrusysid"] . "\"";
							if($paskatinimaiid !=0){
								if(($getpaskatinimai['paskrusysid'])==($pask["paskrusysid"])){
								echo " selected";
								};
							};
							
							echo ">" . $pask["paskrusys"] . "</option>";
						};

			?> 
			</select>	
<BR>			
Naikinama nuobauda:			
<select name="nuobauda" size="1">
			<?php 		$query = "SELECT * 
						FROM `nuobaudos` 
						WHERE userid = ";
						$query .= $_GET['userid'];
						$query .= " ORDER BY svarstdata ASC";
						$nuob_set = mysql_query($query, $connection);
						confirm_query($nuob_set);
						while ($nuob = mysql_fetch_array($nuob_set)) {
							echo "<option value=\"". $nuob["nuobaudosid"] . "\"";
							if($paskatinimaiid !=0){
								if(($getpaskatinimai['nuobaudosid'])==($nuob["nuobaudosid"])){
								echo " selected";
								};
							};
							
							echo ">" . $nuob["svarstdata"] . "</option>";
						};

			?> 
			</select> <small>Pildoma naikinant nuobauda pries termina</small>
<br>
<input name="submit" type="submit" value="SubmiNt" />
</form>

			</article>
            
<?php require("includes/footer.php"); ?>
